package net.adst.ui.login;

import net.adst.resources.Constants;
import net.adst.util.GBC;
import net.adst.workers.AuthenticationWorker;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 01.11.12
 * Time: 4:42
 * To change this template use File | Settings | File Templates.
 */
public class LoginDialog extends JDialog {

    private JTextField loginField;
    private JPasswordField passwordField;
    private JButton signInButton;
    private JLabel statusLabel;

    public LoginDialog() {
        Properties prop = System.getProperties();
//        tmpDir = prop.getProperty("java.io.tmpdir");
        setLayout(new GridBagLayout());
        addLoginForm();
        setCentered();
        setModal(true);
        setTitle(Constants.LOGIN_TITLE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        getRootPane().setDefaultButton(signInButton);
        if (!loginField.getText().equals(""))    {
            passwordField.requestFocusInWindow();
        }else{
            loginField.requestFocusInWindow();
        }

        setColors();

    }

    /**
     * Adds sign in components to dialog
     */
    private void addLoginForm() {
        JLabel loginLabel = new JLabel("Логин:");
        add(loginLabel, new GBC(0, 0).setInsets(5, 5, 5, 0).setAnchor(GBC.WEST));
        loginField = new JTextField(25);
//        loginField.setText(storedLogin);
        add(loginField, new GBC(1, 0).setInsets(5).setFill(GBC.HORIZONTAL));
        JLabel passwordLabel = new JLabel("Пароль:");
        add(passwordLabel, new GBC(0, 1).setInsets(5, 5, 5, 0).setAnchor(GBC.WEST));
        passwordField = new JPasswordField(25);
        add(passwordField, new GBC(1, 1).setInsets(5).setFill(GBC.HORIZONTAL));

        signInButton = new JButton();
        signInButton.setText("Подключиться");
        signInButton.setFocusPainted(false);
        signInButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!loginField.getText().isEmpty()) {
                    AuthenticationWorker worker = new AuthenticationWorker(LoginDialog.this, statusLabel, loginField.getText(), new String(passwordField.getPassword()));
                    worker.execute();
                } else {
                    statusLabel.setText("Введите логин!!!");
                    statusLabel.setForeground(Color.RED);
                }
            }
        });
        add(signInButton, new GBC(0, 5, 2, 1).setInsets(5).setWeight(1, 0));
        statusLabel = new JLabel("Введите логин и пароль");
        add(statusLabel, new GBC(0, 6, 2, 1).setInsets(5));
    }


    public void init() {
        setModalityType(ModalityType.TOOLKIT_MODAL);
        setModalExclusionType(ModalExclusionType.TOOLKIT_EXCLUDE);
        setResizable(false);
        setVisible(true);
        toFront();
        setAlwaysOnTop(true);
    }

    private void setCentered() {
        pack();
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2);
    }

    private void setColors() {
        getContentPane().setBackground(Constants.COLOR_LIGHT_BLUE);
        Component[] components = getContentPane().getComponents();
        for (Component comp : components) {
            if (!(comp instanceof JComboBox) && !(comp instanceof JTextField)) {
                comp.setBackground(Constants.COLOR_LIGHT_BLUE);
            }
        }
    }

}
